/*
Filename    : SqlAdminBackup.sql
Createdate  : 2013-09-16
Author      : Niels Grove-Rasmussen
Description : Create T-SQL prototype of SqlAdminBackup.
              The prototype is implemented with stored procedures.
*/

-- Create database to hold SqlAdminBackup objects
IF EXISTS ( SELECT [name] FROM [master].[sys].[databases] WHERE [databases].[name] = N'sqladmin' )
BEGIN
  DECLARE @errmsg nvarchar(2047) = LEFT(CONVERT(nvarchar(128), GETUTCDATE(), 127), 22) + N'Z  The database [sqladmin] exists already.';
  RAISERROR(@errmsg, 16, 0) WITH NOWAIT;
END
DECLARE @create_db_sqladmin nvarchar(max) = N'CREATE DATABASE [sqladmin] ON  PRIMARY ( NAME = N''sqladmin_system'', ';
SET @create_db_sqladmin += N'FILENAME = N''' + CAST(SERVERPROPERTY('INSTANCEDEFAULTDATAPATH') AS nvarchar(max)) + N'sqladmin_system.mdf'' ), ';
SET @create_db_sqladmin += N'FILEGROUP [SQLADMIN] ( NAME = N''sqladmin'', ';
SET @create_db_sqladmin += N'FILENAME = N''' + CAST(SERVERPROPERTY('INSTANCEDEFAULTDATAPATH') AS nvarchar(max)) + N'sqladmin.ndf'' ) ';
SET @create_db_sqladmin += N'LOG ON ( NAME = N''sqladmin_log'', ';
SET @create_db_sqladmin += N'FILENAME = N''' + CAST(SERVERPROPERTY('INSTANCEDEFAULTLOGPATH') AS nvarchar(max)) + N'sqladmin_log.ldf'' );';
EXECUTE( @create_db_sqladmin );
ALTER DATABASE [sqladmin] MODIFY FILEGROUP [SQLADMIN] DEFAULT;
GO

